package com.efast.dmom.mgmt.openws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cafeauto.ws.Message;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.base.WSAbstractAdapter;
import com.efast.cafe.commponent.es.webservice.utils.W3CElementUtil;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.mes.testline.TestLineDataLogBean;
import com.efast.dmom.mgmt.bean.mes.testline.VoiceData;
import com.efast.dmom.mgmt.bean.mes.testline.VoiceDataBean;
import com.efast.dmom.mgmt.bean.mes.testline.VoiceDataBizContent;
import com.efast.dmom.mgmt.bean.mes.testline.VoiceInboundDataSet;
import com.efast.dmom.mgmt.service.mes.testline.impl.TestLineService;

@CafeWSService(bizCode = "Voice_Test")
public class VoiceDataService extends WSAbstractAdapter{

	@Override
	public Message handleMessage(Message message) {
		
		logger.info("Received voice data from: " + message.getBIZ_CODE());

		VoiceDataBizContent content = W3CElementUtil.elementUnmarshalToObj(message.getBIZ_CONTENT(),
				VoiceDataBizContent.class);
		VoiceInboundDataSet inboundData = content.getVoiceInboudData();
		List<VoiceData> dataList = inboundData.getVoiceData();
		
		if(dataList != null && dataList.size() >0){
			
			List<VoiceDataBean> list = new ArrayList<VoiceDataBean>();
			for (VoiceData data : dataList) {

				VoiceDataBean dataBean = new VoiceDataBean();

				dataBean.setVin(data.getVin());
				dataBean.setVoice(data.getVoice());
				dataBean.setTime(data.getTime());
				dataBean.setVoiceResult(data.getVoiceResult());
				dataBean.setTestNumber(data.getTestNumber());
				
				dataBean.setCreateTime(new Date());
				dataBean.setCreateUser(message.getBIZ_CODE());
				
				list.add(dataBean);
			}

			TestLineService service = SpringContextUtils.getBean(TestLineService.class);
			service.insertVoiceData(list);
			
			TestLineDataLogBean logBean = new TestLineDataLogBean();
			logBean.setVin(dataList.get(0).getVin());
			logBean.setStatus("保存");
			logBean.setReceivedTime(new Date());
			logBean.setSource(message.getMESSAGE_FROM());
			logBean.setDestination(message.getMESSAGE_TO());
			
			service.insertTestLineLog(logBean);
		}
		

		message.setBIZ_CONTENT(null);
		message.setMESSAGE_FROM("MES");
		message.setMESSAGE_TO("Voice_Test");
		message.setRET_CODE("200");
		message.setRET_CODE_DESC("Successful");
		return message;
	}

}
